/*
 * 文件名：StsOutputValueServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月20日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.IncaeSureService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.bpa.service.ProContractService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.sts.service.OutputValueService;
import top.infopub.job.sts.service.StsOutputValueService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.sts.OutputValue;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


@Service
public class StsOutputValueServiceImpl implements StsOutputValueService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private OutputValueService outputValueService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsOutputValueJob() {
        //查询所有项目合同名称
        Map<String, Object> proContractWhere = Maps.newHashMap();
        proContractWhere.put("processStatus", "9");
        List<ProContract> proContracts = proContractService.selectByMapWhere(proContractWhere);
        for (ProContract proContract : proContracts) {
            OutputValue outputValue = new OutputValue();
            //往表里存放项目信息
            outputValue.setProCode(proContract.getProCode());
            outputValue.setProName(proContract.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(proContract.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                outputValue.setOrgProName(orgInfos.get(0).getCompName());
                outputValue.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    outputValue.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    outputValue.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            outputValue.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            outputValue.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            outputValue.setStsMonth(ParserUtil.prevMonth());//上个月
            //当月产值(下浮后)对外(元)
            Map<String, Object> outSureWhere = Maps.newHashMap();
            outSureWhere.put("sureMonth", outputValue.getStsMonth());
            outSureWhere.put("proCode", outputValue.getProCode());
            BigDecimal outSureAfter = outcaeSureService.selectProQaOfOutSure(outSureWhere);
            outputValue.setOutSureAfter(outSureAfter);
            //当月产值(下浮后)对内(元)
            Map<String, Object> innerSureWhere = Maps.newHashMap();
            innerSureWhere.put("sureMonth", outputValue.getStsMonth());
            innerSureWhere.put("proCode", outputValue.getProCode());
            BigDecimal inSureAfter = incaeSureService.selectProQaOfInnerSure(innerSureWhere);
            outputValue.setInSureAfter(inSureAfter);
            //差额(对外-对内)
            BigDecimal diffQuota = outSureAfter.subtract(inSureAfter);
            outputValue.setDiffQuota(diffQuota);
            //数据状态(默认为有效)
            outputValue.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            outputValue.setAddBy("system");
            outputValue.setAddTime(new Date());
            outputValue.setUptBy("system");
            outputValue.setUptTime(new Date());
            outputValueService.insert(outputValue);
        }
    }
}